<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller {

    const adminame = "admin";
    const adminpwd = "123456";

    function __construct() {

        parent::__construct();
        $this->load->helper('url');
        $this->load->database();
        $this->load->helper('func');
    }

    public function index() {       
        $this->load->view("admin/login");
    }

    public function login() {
        $name = $this->input->post("name");
        $pwd = $this->input->post("pwd");
        if (($name == static::adminame) && ($pwd == static::adminpwd)) {
            $_SESSION["statue"]="login";
            redirect("/admin/messagelist/");
        } else {
            redirect("/admin/");
        }
    }

    public function addmessage() {
        $this->isLogin();
        $this->load->view("admin/adminmsgdetail");
    }

    public function addamessage() {
         $this->isLogin();
        $time = time();
        $sql = "insert into message(title,content,time)values(\"{$_POST["title"]}\",\"{$_POST["content"]}\",{$time}) ";
        $this->db->query($sql);
        redirect('/admin/messagelist/', 'refresh');
    }

    public function messagelist() {
         $this->isLogin();
        $sql = "select * from message";
        $query = $this->db->query($sql);
        $row = $query->result_array();
        $data['rows'] = $row;
        $this->load->view("admin/adminmsglist", $data);
    }

    public function messagedit() {
         $this->isLogin();
if(isset($_POST["title"])&&isset($_POST["content"])&&isset($_POST["id"])) {
   $sql="update message set title=\"{$_POST["title"]}\", content=\"{$_POST["content"]}\"  where id=\"{$_POST["id"]}\" ";

        $query = $this->db->query($sql);
      redirect('/admin/messagelist/', 'refresh');
        }
        $id = get_segment_fuc(3);
        $get_single = "select * from message where id=\"{$id}\"";
        $query = $this->db->query($get_single);
        $row = $query->result_array();
        $data['rows'] = $row;
        $this->load->view("admin/adminmsgedit", $data);
    }

    public function messagdel() {
         $this->isLogin();
        $id = get_segment_fuc(3);
        $del = "DELETE FROM message
WHERE id = {$id}";
        $this->db->query($del);
        redirect('/admin/messagelist/', 'refresh');
    }
    private function  isLogin(){
        if($_SESSION["statue"]!="login"){
            
             redirect("/admin/");
        }
        
        
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */